<?php

/**
 * Component that encapsulates the FrontlineSMS Http Trigger functionality
 */
class FrontlineSmsHttpTrigger extends CApplicationComponent {

    protected $_settingsID = 'settings';

    protected $_hostName = 'localhost';

    protected $_portNumber = 8011;

    public function init() {
        $settings = Yii::app()->getComponent($this->getSettingsId());

        $this->setHostName($settings->get('frontlinesms', 'hostName', $this->getHostName()));
        $this->setPortNumber($settings->get('frontlinesms', 'portNumber', $this->getPortNumber()));
    }

    public function getSettingsId() {
        return $this->_settingsID;
    }

    public function setSettingsId($settingsID) {
        $this->_settingsID = $settingsID;
    }

    public function getHostName() {
        return $this->_hostName;
    }

    public function setHostName($hostName) {
        $this->_hostName = $hostName;
    }

    public function getPortNumber() {
        return $this->_portNumber;
    }

    public function setPortNumber($portNumber) {
        $this->_portNumber = $portNumber;
    }

    public function send($destinationPhoneNumber, $message) {
        $url = 'http://' .
                $this->getHostName() .
                ':' .
                $this->getPortNumber() .
                '/send/sms/' .
                urlencode($destinationPhoneNumber) .
                '/' .
                urlencode($message) .
                '/';

        $handle = curl_init();

        curl_setopt($handle, CURLOPT_URL, $url);
        curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 2);
        curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
        $status = curl_exec($handle);
        curl_close($handle);
        
        return $status;
    }

}

?>
